<?php
/**
 * 配置处理
 * @copyright 2020-2021 WillPHP
 * @author NoMind<24203741@qq.com/113344.com>
 * @version WillPHPv3
 * @since 2021-12-01
 */
namespace wiphp;
defined('WIPHP_URI') or die('Access Denied');
class Config {
	protected static $name = 'wiphp_config';
	protected static $config = []; 	
	protected static $loads = ['app', 'database', 'filter', 'route', 'site'];
	//初始化配置
	public static function init() {	
		//检测目录
		if (!is_dir(PATH_CACHE)) mkdir(PATH_CACHE, 0777, true);
		if (!is_dir(PATH_LOG)) mkdir(PATH_LOG, 0777, true);
		if (!is_dir(PATH_VIEWC)) mkdir(PATH_VIEWC, 0777, true);
		if (!is_dir(PATH_SHTML)) mkdir(PATH_SHTML, 0777, true);	
		//生成配置
		$config = Cache::get(self::$name);
		if (!$config) {
			$config = [];
			foreach (self::$loads as $cname) {
				$config[$cname] = self::load($cname);
			}			
			Cache::set(self::$name, $config);
		}
		self::$config = $config;		
		//多主题
		$theme = isset($config['site']['theme'])? $config['site']['theme'] : 'default';		
		define('__THEME__', $theme);
		define('THEME_PATH', (THEME_ON)? PATH_VIEW.'/'.$theme : PATH_VIEW);			
	}
	//加载配置文件
	protected static function load($file) {
		$files = [WIPHP_URI.'/config/'.$file.'.php', APP_PATH.'/config/'.$file.'.php'];
		$config = [];	
		foreach ($files as $file) {
			if (file_exists($file)) {
				$temp = require $file;
				if (is_array($temp)) {
					$config = array_merge($config, $temp);					
				}
			}
		}
		return $config;
	}
	//获取配置
	public static function get($name = '', $default = '') {
		$name = trim($name, '.');
		$config = self::$config;
		if (empty($name)) return $config;
		if (false === strpos($name, '.')) {
			if (isset($config[$name])) {
				return $config[$name];
			}
			return isset($config['site'][$name])? $config['site'][$name] : $default;
		}
		$name = explode('.', $name);
		foreach ($name as $val) {
			if (isset($config[$val])) {
				$config = $config[$val];
			} else {
				return $default;
			}
		}
		return $config;
	}
	//设置配置
	public static function set($name, $val = '') {
		$name = trim($name, '.');
		if (false === strpos($name, '.')) {
			self::$config['site'][$name] = $val;
			return $val;
		}
		$cn = explode('.', $name);
		$count = count($cn);
		if ($count == 2) {
			self::$config[$cn[0]][$cn[1]] = $val;
		} else if ($count == 3) {
			self::$config[$cn[0]][$cn[1]][$cn[2]] = $val;
		} else if ($count > 3) {
			//App::halt('不支持参数：'.$name);
		}
		return $val;
	}
	//删除配置
	public static function del($name) {
		$name = trim($name, '.');
		if (false === strpos($name, '.')) {
			if (isset(self::$config[$name])) {
				unset(self::$config[$name]);
				return true;
			} else if (isset(self::$config['site'][$name])) {
				unset(self::$config['site'][$name]);
				return true;
			}
		}
		$cn = explode('.', $name);
		$count = count($cn);
		if ($count == 2 && isset(self::$config[$cn[0]][$cn[1]])) {
			unset(self::$config[$cn[0]][$cn[1]]);
			return true;
		} elseif ($count == 3 && isset(self::$config[$cn[0]][$cn[1]][$cn[2]])) {
			unset(self::$config[$cn[0]][$cn[1]][$cn[2]]);
			return true;
		}
		return false;
	}	
}